/**
 * deMap - jQuery Plugin for create Google Maps V3
 * 
 * http://www.dedepress.com
 *
 * Copyright (c) 2012 Cloud Stone by dedepress.com
 * Dual licensed under the MIT and GPL licenses.
 *  - http://www.opensource.org/licenses/mit-license.php
 *  - http://www.gnu.org/copyleft/gpl.html
 *
 * @author: Cloud Stone
 * @version: 1.0
 * Date: June 05, 2012
 */

(function($) {
	var deMapRand = function(){
		return (new Date().getTime()) ^ Math.random();
	};

	$.fn.deMap = function(options) {
		// Default settings by plugin
		var settings = {
			zoom: 8,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			lat: '0',
			lng: '0',
			marker: true,
			infowindow: false
		};
		
		var dup = [deMapRand()];
		 
		return this.each(function() {
			// Get default settings from html tag and attritube
			var defaults = {};
			
			if($(this).attr('data-lat'))
				defaults.lat = $(this).attr('data-lat');
			$(this).attr('data-lng')
				defaults.lng = $(this).attr('data-lng');
			$(this).attr('data-zoom')
				defaults.zoom = $(this).attr('data-zoom');
			defaults.address = $(this).text();
			defaults.infoContent = $(this).text();

			$.extend(settings, defaults);
			
			if (options)
				$.extend(settings, options);
			
			// Before callback
			if (typeof settings.before == 'function') {
				settings.before.call(this, settings);
			}
			
			settings.lat = parseFloat(settings.lat);
			settings.lng = parseFloat(settings.lng);
			settings.zoom = parseInt(settings.zoom);
			var latlng = new google.maps.LatLng(settings.lat, settings.lng);
			settings.center = latlng;

			// Set diffient id
			if ($(this).attr('id')) {
				id = $(this).attr('id');
			} else {
				rand = dup.pop()+1;
				id = "demap" + rand;
				dup.push(rand);
			}
			$(this).attr('id', id);
			var map = new google.maps.Map(document.getElementById(id), settings);
			
			var marker = '';
				
			if(parseFloat(settings.lat) != 0 && parseFloat(settings.lng) != 0) {
				var marker = addMarker(settings, map);
				addInfoWindow(settings, marker, map);
			} else {
				// GeoCoder
				var geocoder = new google.maps.Geocoder();
			
				geocoder.geocode({'address':settings.address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						map.setCenter(results[0].geometry.location);

						var marker = addMarker(settings, map);
						addInfoWindow(settings, marker, map);
					}
				});
			}

			// After callback
			if (typeof settings.after == 'function') {
				settings.after.call(this, map);
			}
		});
	
		function addMarker(options, map) {
			var marker = new google.maps.Marker({
				map: map, 
				title: options.address,
				position: map.getCenter()
			});
			
			return marker;
		};

		function addInfoWindow(options, marker, map) {
			if(options.infowindow == false)
				return false;
			
			var infowindow;
			if (options.infowindow instanceof google.maps.InfoWindow) {
				infowindow = options.infowindow;
			} else if(options.infowindow == true) {
				infowindow = new google.maps.InfoWindow({
					content: options.infoContent
				});
			}
			
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map, marker);
			});
		};

	};
})(jQuery);